<%var styleBlock={%>
<style>
    .dialogStyle {
        width: 550px;
        height: 450px;
    }
</style>
<%};%>

<% var mainBlock={%>
<!--列表-->
<table id="dg" style="width:100%;height:85%"
       url="${ctx}/admin/auth/role/query.do"
       toolbar="#toolbar"
       rownumbers="false" fitColumns="false" pagination="true">
</table>
<div id="toolbar">
    <%for (b in buttons) {%>
    <%if(b.isShow){%>
    <a href="#" class="easyui-linkbutton" iconCls="${b.icon}" plain="true" onclick="_${b.description}()">${b.name}</a>
    <%}%>
    <%}%>

    <!--搜索栏-->
    <div class="padding5px" id="search">
        角色名:
        <input name="_rolename"  class="easyui-textbox"/>

        按状态:
        <select class="easyui-combobox" name="_enable">
            <option value="" selected>全部</option>
            <option value="1">有效</option>
            <option value="0">无效</option>
        </select>
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="_search()">搜索</a>
    </div>
</div>

<!--新增对话框-->
<div id="create-dlg" class="easyui-dialog dialogStyle"
     data-options="iconCls:'icon-save',maximizable: true,resizable:true,modal:true" style="width: 550px"
     closed="true" buttons="#create-dlg-buttons">
    <div class="ftitle"></div>
    <form id="create-fm" method="post">
        <div class="fitem padding5px">
            <label class="formLabel">角色名:</label>
            <input name="name" class="easyui-textbox formInputText" required="true">
        </div>
        <div class="fitem padding5px">
            <label class="formLabel">描述:</label>
            <input name="description" data-options="multiline:true" required="true" class="easyui-textbox formInputText"
                   style="height: 60px"/>
        </div>
        <div class="fitem padding5px">
            <label class="formLabel">等级:</label>
            <select class="easyui-combobox" name="level" required="true">
                <%for(level in levels){%>
                <option value="${@level.getName()}">${@level.getName()}</option>
                <%}%>
            </select>
        </div>
        <div class="fitem padding5px">
            <label class="formLabel">菜单与按钮:</label>
            <ul style="list-style-type: none;margin-left: 60px">
                <% var j=0 ;for (m in ms){ %>
                <li><input name="menus[${j++}].id" type="checkbox" value="${m.id}">&nbsp;${m.name}</li>
                <% for(n in m.children) {%>
                <li>&nbsp;&nbsp;&nbsp;<input name="menus[${j++}].id" type="checkbox" value="${n.id}">&nbsp;${n.name}
                </li>
                <%for(b in n.children) {%>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="menus[${j++}].id" class="menus" type="checkbox"
                                                           value="${b.id}">&nbsp;${b.name}
                <%}%>
                <%}%>
                <%}%>


            </ul>
        </div>

        <div class="fitem padding5px">
            <label class="formLabel">状态：</label>
            <select class="easyui-combobox" name="enable" required="true">
                <option value="1" selected>有效</option>
                <option value="0" >无效</option>
            </select>
        </div>
    </form>
</div>
<div id="create-dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6 width90px" iconCls="icon-ok" onclick="_createSave()">确定</a>
    <a href="javascript:void(0)" class="easyui-linkbutton width90px" iconCls="icon-cancel"
       onclick="javascript:$('#create-dlg').dialog('close')">取消</a>
</div>

<!--编辑对话框-->
<div id="edit-dlg" class="easyui-dialog dialogStyle"
     data-options="iconCls:'icon-save',maximizable: true,resizable:true,modal:true" style="width: 550px"
     closed="true" buttons="#edit-dlg-buttons">
    <div class="ftitle"></div>
    <form id="edit-fm" method="post">
        <input type="hidden" name="id"/>

        <div class="fitem padding5px">
            <label class="formLabel">角色名:</label>
            <input name="name" class="easyui-textbox formInputText" required="true">
        </div>
        <div class="fitem padding5px">
            <label class="formLabel">描述:</label>
            <input class="easyui-textbox formInputText" name="description" data-options="multiline:true"
                   style="height: 60px"/>
        </div>
        <div class="fitem padding5px">
            <label class="formLabel">等级:</label>
            <select id="sel" class="easyui-combobox" name="level" required="true">
                <%for(level in levels){%>
                <option value="${@level.getName()}">${@level.getName()}</option>
                <%}%>
            </select>
        </div>
        <div class="fitem padding5px">
            <label class="formLabel">菜单与按钮:</label>
            <ul style="list-style-type: none;margin-left: 60px">
                <% j=0 ;for (m in ms){ %>
                <li><input name="menus[${j++}].id" type="checkbox" class="menus" value="${m.id}">&nbsp;${m.name}</li>
                <% for(n in m.children) {%>
                <li>&nbsp;&nbsp;&nbsp;<input name="menus[${j++}].id" class="menus" type="checkbox" value="${n.id}">&nbsp;${n.name}
                </li>
                <%for(b in n.children) {%>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="menus[${j++}].id" class="menus" type="checkbox"
                                                           value="${b.id}">&nbsp;${b.name}
                <%}%>
                <%}%>
                <%}%>
            </ul>
        </div>


        <div class="fitem padding5px">
            <label class="formLabel">状态：</label>
            <select class="easyui-combobox" name="enable">
                <option value="1" selected>有效</option>
                <option value="0" >无效</option>
            </select>
        </div>
    </form>
</div>
<div id="edit-dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6 width90px" iconCls="icon-ok" onclick="_editSave()">确定</a>
    <a href="javascript:void(0)" class="easyui-linkbutton width90px" iconCls="icon-cancel"
       onclick="javascript:$('#edit-dlg').dialog('close')">取消</a>
</div>
<%};%>

<% var scriptBlock={%>
<script>
    //表格
    $(function () {
        $('#dg').datagrid({
            columns:[[
                {field:'ck',checkbox:true},
                {field: 'id', title: '编号'},
                {field: 'name', title: '名字'},
                {field: 'description', title: '描述'},
                {field: 'level', title: '级别'},
                {
                    field: 'enable', title: '状态',
                    formatter: function(value,row,index){
                        if(row.enable)
                            return '有效';
                        else
                            return '无效';
                    }
                },
            ]]
        });
    });





    //新增
    function _create() {
        $('#create-dlg').dialog('open').dialog('setTitle', '新建');
        $('#create-fm').form('clear');
    }

    //新增保存
    function _createSave() {
        $('#create-fm').form('submit', {
            url: '${ctx}/admin/auth/role/editSubmit.do',
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
                var result = eval('('+result+')');
                if (1==result.code && true==result.returnObject){
                    $('#create-dlg').dialog('close');		// close the dialog
                    $('#dg').datagrid('reload');	// reload the user data
                    $.messager.show({
                        title: '成功',
                        msg: '操作成功'
                    });
                } else {
                    $.messager.show({
                        title: '错误',
                        msg: result.message
                    });
                }
            }
        });
    }

    //编辑
    function _edit() {
        var rows = $('#dg').datagrid('getSelections');

        if(rows.length!=1) {
            $.messager.alert('注意',window.edit_msg);
            return;
        }

        var row = $('#dg').datagrid('getSelected');
        if (row){
            $('#edit-dlg').dialog('open').dialog('setTitle', '编辑');
            $.post('${ctx}/admin/auth/role/edit.do?id='+row.id,function(data) {
                $('#edit-fm').form('clear');
                $('#edit-fm').form('load', data);
                //处理菜单与权限的checkbox选中问题
                var menus = data.menus;
                var permissions= data.permissions;
                var buttons = data.buttons;

                for(var m in menus) {
                    $('.menus').each(function(index,element) {
                        if (menus[m].id == $(element).val()) {
                            $(element).prop('checked', true);
                            return;
                        }

                    });
                }
                //管理员等级
                $('#sel').combobox({
                    onLoadSuccess: function () {
                        $('#sel').combobox('select', data.level);
                    }
                });
            });


        }
    }

    //编辑保存
    function _editSave() {
        $('#edit-fm').form('submit', {
            url: '${ctx}/admin/auth/role/editSubmit.do',
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
                var result = eval('('+result+')');
                if (1==result.code && true==result.returnObject){
                    $('#edit-dlg').dialog('close');		// close the dialog
                    $('#dg').datagrid('reload');	// reload the user data
                    $.messager.show({
                        title: '成功',
                        msg: '操作成功'
                    });
                } else {
                    $.messager.show({
                        title: '错误',
                        msg: result.message
                    });
                }
            }
        });
    }

    //删除操作
    function _delete() {
        var rows = $('#dg').datagrid('getSelections');
        var i = 0;
        var rowStr = "";
        for(i;i<rows.length;i++){
            rowStr += rows[i].id;
            if(i < rows.length-1){
                rowStr += ',';
            }else{
                break;
            }
        }
        if (null !=rowStr && rowStr.length>0) {
            $.messager.confirm('注意', '你确定要删除吗?', function (r) {
                if (r) {
                    $.post('${ctx}/admin/auth/role/delete.do', {ids: rowStr}, function (result) {
                        if (1==result.code && true==result.returnObject) {
                            $('#dg').datagrid('reload');    // reload the user data
                        } else  {
                            $.messager.show({    // show error message
                                title: '错误',
                                msg: result.message
                            });
                        }
                    }, 'json');
                }
            });
        }
    }

    //搜索
    function _search() {
        var jsonArr = new Array();

        var _rolename = $("#search").find("input[name='_rolename']").val();
        var _enable = $("#search").find("input[name='_enable']").val();

        if (null != _rolename && _rolename.length > 0) {
            var jsonObj = new Object();
            jsonObj.fieldName = "name";
            jsonObj.value = _rolename
            jsonObj.operator = "like"
            jsonArr.push(jsonObj);
        }

        if (null != _enable && _enable.length > 0) {
            var jsonObj = new Object();
            jsonObj.fieldName = "enable";
            jsonObj.value = _enable
            jsonObj.operator = "eq"
            jsonArr.push(jsonObj);
        }

        $('#dg').datagrid('load', {
            'searchParams': JSON.stringify(jsonArr),
        });
    }



</script>
<%};%>

<%
var layoutParas = {"mainBlock":mainBlock,"styleBlock":styleBlock,"scriptBlock":scriptBlock};
includeFileTemplate('/layout/admin/template.htm',layoutParas){};
%>